package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserInfoParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

	@Autowired
	private IStrategyService strategyService;
	@Autowired
	private IStrategyThemeService strategyThemeService;
	@Autowired
	private IStrategyCommentService strategyCommentService;

	@GetMapping("/query")
	public Object query(StrategyQuery qo){
		Page<Strategy> page= strategyService.queryByDestId(qo);
		return JsonResult.success(page);
	}

	@GetMapping("/detail")
	public Object detail(String id){
		Strategy strategy = strategyService.get(id);
		return JsonResult.success(strategy);
	}

	@GetMapping("/themes")
	public Object themes(){
		return JsonResult.success(strategyThemeService.list());
	}

	// 发送评论
	@PostMapping("/addComment")
	@RequireLogin
	public Object addComment(StrategyComment strategyComment , @UserInfoParam UserInfo userInfo){
		AssertUtil.hasLength(strategyComment.getContent(),"评论内容必填");
		// 把userInfo中的字段的值复制到strategyComment中,但是必须是两个对象中同名的字段
		BeanUtils.copyProperties(userInfo, strategyComment);
		// 设置用户id
		strategyComment.setUserId(userInfo.getId());
		strategyCommentService.addComment(strategyComment);

		return JsonResult.success();
	}
	// 评论点赞
	@PostMapping("/commentThumb")
	@RequireLogin
	public Object commentThumb(String cid,@UserInfoParam UserInfo userInfo){
		strategyCommentService.commentThumb(cid,userInfo);
		return JsonResult.success();
	}
	// 评论分页
	@GetMapping("/comments")
	public Object comments(StrategyCommentQuery qo){
		return JsonResult.success(strategyCommentService.query(qo));
	}

	// 统计vo量
	@GetMapping("/statisVo")
	public Object statisVo(String sid){
		return JsonResult.success(strategyService.statisVo(sid));
	}
	// 收藏/取消
	@PostMapping("/favor")
	@RequireLogin
	public Object favor(String sid,@UserInfoParam UserInfo userInfo){
		boolean ret = strategyService.strategyFavor(sid,userInfo.getId());
		return JsonResult.success(ret);
	}
	// 顶
	@PostMapping("/strategyThumbup")
	@RequireLogin
	public Object strategyThumbup(String sid,@UserInfoParam UserInfo userInfo){
		boolean ret = strategyService.strategyThumbup(sid,userInfo.getId());
		return JsonResult.success(ret);
	}
}
